<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class album_PhotosController extends Cms_Controller_Action
{
	public function init()
	{
		if($_SESSION["sess_userid"]=="")
		{
		$this->_redirect('/home/');
		}
	  $this->view->siteTitle = 'Photos';
	  $this->currentPage = 1;
	  $this->resultsPerPage = 12;
			
	}
	public function indexAction()
	{
	  	
	 }
	 	public function allAction()
	{
		//echo 'sfsd';exit;
	  	if(!isset($_POST['filter']))
		{
			//echo '<pre>';print_r($_POST);exit;
			//echo 'sdfsdf';exit;
			$memid = $_SESSION["sess_userid"];
			$this->view->params ="";
			$result= Doctrine::getTable('Photos')->getPersonalPhotos($memid,$this->currentPage,$this->resultsPerPage);
			//echo '<pre>';print_r($result);exit;
			$this->view->getPhotos=$result;
			$this->view->type=2;
		} else {
			//echo '<pre>';print_r($_POST);exit;
			$filtr = $_POST['filter'];
			if(isset($_POST['search_key']) && $_POST['search_key']!='')
			{
				$serValue = $_POST['search_key'];
			} else {
				$serValue = '';
			}
			$memid = $_SESSION["sess_userid"];
			//echo $filtr;exit;
			if($filtr == 3)
			{
				$result= Doctrine::getTable('Photos')->getSharedPhotos($memid,$this->currentPage,$this->resultsPerPage,$serValue);
			} else if($filtr == 2)
			{
				$result= Doctrine::getTable('Photos')->getPersonalPhotos($memid,$this->currentPage,$this->resultsPerPage,$serValue);
				
			} else if($filtr == 1)
			{
				$result= Doctrine::getTable('Photos')->getAllPhotos($memid,$this->currentPage,$this->resultsPerPage,$serValue);
			} else if($filtr == 4)
			{
				$result= Doctrine::getTable('Photos')->geSharetoMePhotos($memid,$this->currentPage,$this->resultsPerPage,$serValue);
				//echo '<pre>';print_r($result);exit;
			} else if($filtr == 5)
			{
				$result= Doctrine::getTable('Photos')->gettoSharePhotos($memid,$this->currentPage,$this->resultsPerPage,$serValue);
				//echo '<pre>';print_r($result);exit;
			}
			//echo '<pre>';print_r($result);exit;
			$this->view->getPhotos=$result;
			$this->view->type=$filtr;
			$this->view->params ="";
			$jsonData['output'] = $result;
			$jsonData['pages'] = $result ['pages'];
			$jsonData['pager'] = $result ['pager'];
			$jsonData['currentPage'] = $result ['currentPage'];
			$jsonData['resultsCount'] = $result ['resultsCount'];
			if($filtr == 1)
			{
				$jsonData['output'] = $this->view->render('photos/share-list.html');
				$this->_helper->json->sendJson($jsonData);	
				
			} else if($filtr == 2)
			{
				$jsonData['output'] = $this->view->render('photos/share-list.html');
				$this->_helper->json->sendJson($jsonData);	
				
			} else if($filtr == 3) {
				$jsonData['output'] = $this->view->render('photos/share-list.html');
				$this->_helper->json->sendJson($jsonData);			
			} else if($filtr == 4)
			{
				$jsonData['output'] = $this->view->render('photos/share-list.html');
				$this->_helper->json->sendJson($jsonData);	
				
			} else if($filtr == 5)
			{
				$jsonData['output'] = $this->view->render('photos/share-list.html');
				$this->_helper->json->sendJson($jsonData);	
				
			}			
		
		}
		
			
	}
	public function paginationAction()
	{
		$type=$this->_getParam('id');
		//	echo $type;exit;
		$memid=$_SESSION["sess_userid"];
		$this->view->params ="";
		$this->view->type=$type;
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');
		$serValue = $this->_getParam('orderby') == ''? '':$this->_getParam('orderby');
		if($serValue==0)
		{
			$serValue='';
		}
		if($type == 1)
		{
		$ideaDetails = Doctrine::getTable('Photos')->getAllPhotos($memid,$currentPage,$resultsPerPage,$serValue);
		$this->view->getPhotos = $ideaDetails;
		$jsonData['output'] = $this->view->render('photos/personal-list.html');
		}
		else if($type == 2)
		{
			$ideaDetails= Doctrine::getTable('Photos')->getPersonalPhotos($memid,$currentPage,$resultsPerPage,$serValue);
			$this->view->getPhotos = $ideaDetails;
			$jsonData['output'] = $this->view->render('photos/personal-list.html');
		}
		else if($type == 3)
		{
			$ideaDetails= Doctrine::getTable('Photos')->getSharedPhotos($memid,$currentPage,$resultsPerPage,$serValue);
			$this->view->getPhotos = $ideaDetails;
			$jsonData['output'] = $this->view->render('photos/share-list.html');
		}
		else if($type == 4)
		{
			$ideaDetails= Doctrine::getTable('Photos')->geSharetoMePhotos($memid,$currentPage,$resultsPerPage,$serValue);
			$this->view->getPhotos = $ideaDetails;
			$jsonData['output'] = $this->view->render('photos/share-list.html');
		}
		else if($type == 5)
		{
			$ideaDetails= Doctrine::getTable('Photos')->gettoSharePhotos($memid,$currentPage,$resultsPerPage,$serValue);
			echo '<pre>';print_r($ideaDetails);exit;
			$this->view->getPhotos = $ideaDetails;
			$jsonData['output'] = $this->view->render('photos/share-list.html');
		}
		else
		{
			$photo_id=$type;
			$ideaDetails= Doctrine::getTable('EmComments')->getComments($photo_id,$currentPage,$resultsPerPage);
			$this->view->getComments = $ideaDetails;
			$jsonData['output'] = $this->view->render('photos/photo-comments.html');
		}
		//echo '<pre>';print_r($ideaDetails);exit;
		$jsonData['pages'] = $ideaDetails['pages'];
	    $jsonData['pager'] = $ideaDetails['pager'];
	    $jsonData['currentPage'] = $ideaDetails['currentPage'];
		$jsonData['resultsCount'] = $ideaDetails['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		//echo '<pre>';print_r($ideaDetails);exit;
		
	
	}

			
	public function photosViewAction()
	{
	  	$type=base64_decode($this->_getParam('id'));
		$photo_diffid=base64_decode($this->_getParam('current_page'));

			//echo $trigger_id;exit;
			$memid = $_SESSION["sess_userid"];
			$read_status= Doctrine::getTable('PhotoRelations')->updatePhotoReadStatus($memid,$photo_diffid,1);
			$this->view->params ="photo_diffid=".base64_encode($photo_diffid);
			if($type == 3)
			{
				$view_result= Doctrine::getTable('Photos')->getSharedPhotosByPhotoId($memid,$photo_id);
			} else if($type == 2)
			{
				$view_result= Doctrine::getTable('Photos')->getPersonalPhotosByPhotoId($memid,$photo_diffid,1,12);
				
			} else if($type == 1)
			{
				$view_result= Doctrine::getTable('Photos')->getAllPhotosByPhotoId($memid,$photo_diffid);
			} else if($type == 4)
			{
				$view_result= Doctrine::getTable('Photos')->geSharetoMePhotosByPhotoId($memid,$photo_diffid,1,12);
			} else if($type == 5)
			{
				$view_result= Doctrine::getTable('Photos')->gettoSharePhotosByPhotoId($memid,$photo_diffid);
				//echo '<pre>';print_r($result);exit;
			}
			//echo '<pre>';print_r($view_result);exit;
			//echo '<pre>';print_r($photo_result);exit;
			$this->view->photoview=$view_result;
			$this->view->imgpath= '/'.$this->CONFIG->path->albumPhotosBig;
			$this->view->photo_diffid=$photo_diffid;
			$this->view->photo_id=$view_result['result'][0]['photo_id'];
			$this->view->comments_allow=$view_result['result'][0]['comments_allow'];
			$this->view->type1=0;
			$this->view->type=$type;
			$this->view->current_photo=$current_photo;	
			
	}
	public function addPhotosAction()
	{
			
			$this->_helper->layout->disableLayout();
			$type="";
			$memid = $_SESSION["sess_userid"];
			$group_type="Triggers";
			$photo_diffid=$this->_getParam('photo_diffid');
			$type=$this->_getParam('type');
			$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			$this->view->photo_diffid=$photo_diffid;
			$this->view->type=$type;
			$this->view->groups= $groups;
			$this->view->count= count($groups);
			
	}
	public function favoriteAction()
	{
		//echo '<pre>';print_r($_POST);exit;
	  	$memid = $_SESSION["sess_userid"];
		$photo_id=$_POST["photo_id"];
		$status=$_POST["status"];
		$type=$_POST["type"];
		if($status==1)
		{
		 //trig $status;exit;
		$result= Doctrine::getTable('MyFavorites')->addToFavorite($memid,$photo_id,$type);
		//echo '<pre>';print_r($result);exit;
		}
		else
		{
			if($status==0)
			{
				//echo "inactive";exit;
				$result= Doctrine::getTable('MyFavorites')->inactiveFavorite($memid,$photo_id,$type);
				//echo '<pre>';print_r($result);exit;
			}
		}
		$this->view->status=$status;
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function favoritesAction()
	{
		//echo '<pre>';print_r($_POST);exit;
	  	$memid = $_SESSION["sess_userid"];
		$photo_id=$_POST["photo_id"];
		$status=$_POST["status"];
		$type1=$_POST["type1"];
		$type=$_POST["type"];
		$photo_diffid=$_POST["photo_diffid"];
		
		if($status==1)
		{
		 //trig $status;exit;
		$result= Doctrine::getTable('MyFavorites')->addToFavorite($memid,$photo_id,$type1);
		//echo '<pre>';print_r($result);exit;
		}
		else
		{
			if($status==0)
			{
				//echo "inactive";exit;
				$result= Doctrine::getTable('MyFavorites')->inactiveFavorite($memid,$photo_id,$type1);
				//echo '<pre>';print_r($result);exit;
			}
		}
		$this->view->status=$status;
		
		if($type == 2)
			{
				$view_result= Doctrine::getTable('Photos')->getPersonalPhotosByPhotoId($memid,$photo_diffid,1,12);
				
			} else if($type == 4)
			{
				$view_result= Doctrine::getTable('Photos')->geSharetoMePhotosByPhotoId($memid,$photo_diffid,1,12);
			}
			$this->view->photoview=$view_result;
			$this->view->imgpath= '/'.$this->CONFIG->path->albumPhotosBig;
			$this->view->photo_diffid=$photo_diffid;
			$this->view->photo_id=$view_result['result'][0]['photo_id'];
			$this->view->type1=0;
			$this->view->type=$type;
			$jsonData['output'] = $this->view->render('photos/photo-view-list.html');
			$this->_helper->json->sendJson($jsonData);
			
	}
	public function deletesharedPhotosAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$memid = $_SESSION["sess_userid"];
		$photo_id = $_POST["photo_id"];
		$photo_diffid = $_POST["photo_diffid"];
		$is_owner = $_POST["is_owner"];
		//echo $id."  and  ".$action;exit;
		if($is_owner == 1)
		{
		$result= Doctrine::getTable('PhotoRelations')->deleteOwnerPhotos($photo_id,$photo_diffid,$memid);
		}
		else
		{		
			
			$result= Doctrine::getTable('PhotoRelations')->deleteSharedPersonPhotos($photo_id,$photo_diffid,$memid);
			//echo $result;exit;
			
		}
		//echo '<pre>';print_r($result);exit;
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function deletesharedPhotoAction()
	{
		//echo '<pre>';print_r($_POST);exit;

	  	$memid = $_SESSION["sess_userid"];
		$id = $_POST["val"];
		$action = $_POST["action"];
		//echo $id."  and  ".$action;exit;
		if($action == "photo_id")
		{
		$result= Doctrine::getTable('PhotoRelations')->deletePhotoByPhotoId($id);
		}
		else
		{		
			if($action == "relation_id")
			{
			$result= Doctrine::getTable('PhotoRelations')->deletePhotoByRelationId($id);
			//echo $result;exit;
			}
		}
		//echo '<pre>';print_r($result);exit;
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function deletepersonalPhotoAction()
	{
		//echo '<pre>';print_r($_POST);exit;
	  	$memid = $_SESSION["sess_userid"];
		$relation_id = $_POST["relation_id"];
			$result= Doctrine::getTable('PhotoRelations')->deletePhotoByRelationId($relation_id);
			//echo $result;exit;
		//echo '<pre>';print_r($result);exit;
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function addtoAlbumAction()
	{
			//echo '<pre>';print_r($_POST);exit;
			$this->_helper->layout->disableLayout();
			$photos=$this->_getParam('photos');
			$photo_diffid=$this->_getParam('photo_diffid');
			$type=$this->_getParam('type');
			//echo '<pre>';print_r($photos);exit;
			$memid = $_SESSION["sess_userid"];
			$albums = Doctrine::getTable('AlbumCategory')->getAlbumsByUserId($memid);
			//echo '<pre>';print_r($albums);exit;
			$this->view->getAlbums= $albums;
			$this->view->photos= $photos;
			$this->view->photo_diffid= $photo_diffid;
			$this->view->type= $type;
			$this->view->count= count($albums);
	}
	public function addPhotosToalbumAction()
	{
			
			//echo '<pre>';print_r($_POST);exit;
			$photos=$this->_getParam('id');
			$photo_diffid=$this->_getParam('current_page');
			$type=$this->_getParam('results_per_page');
			$photo = trim($photos,',');
			$photo_ids=explode(',',$photo);
			//echo '<pre>';print_r($photo_names);exit;
			$memid = $_SESSION["sess_userid"];
			$org_type="P";
			$diffId=time();
			foreach($photo_ids as $photo_id)
			{
			$photo_names = Doctrine::getTable('Photos')->getPhoto($photo_id);
			//echo '<pre>';print_r($photo_names);exit;
			$result[] = Doctrine::getTable('Photos')->addPhoto($memid,$org_type,$_POST['albumName'],$photo_names['photo'],$diffId);
			}
			$this->_redirect("/album/photos/photos-view/".base64_encode($type)."/".base64_encode($photo_diffid));
			
	}
	public function deleteSelectedPhotosAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$photo_ids=trim($_POST['photo_id'],',');
		$delete_type=$_POST['delete_type'];
	  	$memid = $_SESSION["sess_userid"];
		//echo '<pre>';print_r($photo_ids);exit;
		//$relation_id = $_POST["relation_id"];
		if($delete_type == "photo_id")
		{
			//echo "xxxxxx";
			$result= Doctrine::getTable('PhotoRelations')->deleteSelectedPhotos($memid,$photo_ids);
		}
		else 
		{
			//echo "relation_id";exit;
			$result= Doctrine::getTable('PhotoRelations')->deleteSelectedPhotosByRelationId($memid,$photo_ids);
		}
		//echo $result;exit;
		//echo '<pre>';print_r($result);exit;
		//$this->_redirect("/album/photos/all");
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
		public function deleteSelectedPhotoAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$photo_ids=trim($_POST['photo_id'],',');
		$delete_type=$_POST['delete_type'];
	  	$memid = $_SESSION["sess_userid"];
		$type=$_POST["type"];
		$photo_diffid=$_POST["photo_diffid"];
		//echo '<pre>';print_r($photo_ids);exit;
		//$relation_id = $_POST["relation_id"];
		if($delete_type == "photo_id")
		{
			//echo "xxxxxx";
			$result= Doctrine::getTable('PhotoRelations')->deleteSelectedPhotos($memid,$photo_ids);
		}
		else 
		{
			//echo "relation_id";exit;
			$result= Doctrine::getTable('PhotoRelations')->deleteSelectedPhotosByRelationId($memid,$photo_ids);
		}
		//echo $result;exit;
		//echo '<pre>';print_r($result);exit;
		//$this->_redirect("/album/photos/all");
		if($type == 2)
			{
				$view_result= Doctrine::getTable('Photos')->getPersonalPhotosByPhotoId($memid,$photo_diffid,1,12);
				
			} else if($type == 4)
			{
				$view_result= Doctrine::getTable('Photos')->geSharetoMePhotosByPhotoId($memid,$photo_diffid,1,12);
			}
			$this->view->photoview=$view_result;
			$this->view->imgpath= '/'.$this->CONFIG->path->albumPhotosBig;
			$this->view->photo_diffid=$photo_diffid;
			$this->view->photo_id=$view_result['result'][0]['photo_id'];
			$this->view->type1=0;
			$this->view->type=$type;
			$jsonData['output'] = $this->view->render('photos/photo-view-list.html');
			$this->_helper->json->sendJson($jsonData);
			
	}
	public function deletePhotosAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$photo_ids=$_POST['photo_id'];
		$photo_diffid=$_POST['photo_diffid'];
		$delete_type=$_POST['delete_type'];
	  	$memid = $_SESSION["sess_userid"];
		//echo '<pre>';print_r($photo_ids);exit;
		//$relation_id = $_POST["relation_id"];
		if($delete_type == "photo_id")
		{
			//echo "xxxxxx";
			$result= Doctrine::getTable('PhotoRelations')->deleteSelectedPhotos($memid,$photo_ids);
		}
		else 
		{
			//echo "relation_id";exit;
			$result= Doctrine::getTable('PhotoRelations')->deleteSelectedPhotosByDiffId($memid,$photo_ids,$photo_diffid);
		}
		//echo $result;exit;
		//echo '<pre>';print_r($result);exit;
		//$this->_redirect("/album/photos/all");
		$jsonData['output'] = $result;
		$this->_helper->json->sendJson($jsonData);
			
	}
	public function sharePhotosAction()
	{
		//echo '<pre>';print_r($result);exit;
		$this->_helper->layout->disableLayout();
		$memid = $_SESSION["sess_userid"];
		$photos=$this->_getParam('photos');
		$photo_diffid=$this->_getParam('photo_diffid');
		$type=$this->_getParam('type');
		//echo $photos." and photo_diffid ".$photo_diffid."and type".$type;exit;
		$group_type="triggers";
		$groups = Doctrine::getTable('Groups')->getGroupsById($memid,$group_type,1);
			//echo '<pre>';print_r($groups);exit;
			$this->view->groups= $groups;
			$this->view->count= count($groups);
			$this->view->photos= $photos;
			$this->view->photo_diffid= $photo_diffid;
			$this->view->type= $type;
			
				//end 	
	}
	public function getContactMailsAction()
	{
		$memid = $_SESSION["sess_userid"];
		$a = Doctrine::getTable('Contacts')->getMails($memid);
				//echo '<pre>';print_r($a);exit;
				$jsonData = array();
				if(count($a)>0)
				{
					foreach($a as $key=>$mails)
					{
						$jsonData[$key]['key'] = $mails['email'];
						$jsonData[$key]['value'] = $mails['email'];					
					}
				}
				$this->_helper->json->sendJson($jsonData);
	}
	public function addSharePhotosAction()
	{
		//echo '<pre>';print_r($_POST);exit;
			$photos=$this->_getParam('id');
			$timediffId=$this->_getParam('current_page');
			$type1=$this->_getParam('results_per_page');
			$photo = trim($photos,',');
			$photo_ids=explode(',',$photo);
			//echo '<pre>';print_r($photo_names);exit;
			$memid = $_SESSION["sess_userid"];
			$org_type="P";
			$data=$_POST;
				$data['event']=0;
				//echo '<pre>';print_r(base64_encode($type));exit;
			if($_POST['radio'] == "group")
			{
					//$result="";
					foreach($_POST['group'] as $group_id)
					{
						$result[]= Doctrine::getTable('GroupDetails')->getGroupDetailsByGroupId($group_id);
					}
					//echo '<pre>';print_r($result);exit;
				foreach($photo_ids as $photo_id)
				{
						$type="P";$album_id=0;
						$is_owner=0;
					foreach($result as $r1)
					{
						foreach($r1 as $r2)
						{
							$is_owner=0;$album_id=0;
							$res= Doctrine::getTable('PhotoRelations')->checkPhotoRelation($photo_id,$r2['user_id']);
							//echo '<pre>';print_r($res);exit;
							if($res['numCount']==0)
							{
							$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$r2['user_id'],$is_owner,$r2['group_id'],$data['event'],$timediffId);
							//echo $r."   ".$r1;exit;
							$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "Album notification from EmotionsMirror";
							$Message = 'Hello "'.$r2['user_name'].'",<br/><br/>you have AlbumNotification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							//sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
							}
						}
					}
				}
					$this->_redirect("/album/photos/photos-view/".base64_encode($type1)."/".base64_encode($timediffId));
			}

				else if($_POST['radio']="public")
				{
					foreach($photo_ids as $photo_id)
					{
						$type="P";$album_id=0;
						$is_owner=0;
						$result= Doctrine::getTable('Contacts')->getContacts($memid);
						//echo '<pre>';print_r($result);exit;
						foreach($result as $contact_id)
						{
							$is_owner=0; $group_id=0;
							//$notifications = Doctrine::getTable('Notifications')->getNotifications($contact_id['contact_id']);
							//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
							$result= Doctrine::getTable('PhotoRelations')->checkPhotoRelation($photo_id,$contact_id['contact_id']);
							if($result['numCount']==0)
							{
							$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$contact_id['contact_id'],$is_owner,$group_id,$data['event'],$timediffId);
							}
							//echo $r."   ".$r1;exit;
							//if($notifications['others_shared_triggers'] !="off")
							
							$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "Album notification from EmotionsMirror";
							$Message = 'Hello "'.$contact_id['first_name'].'",<br/><br/>you have AlbumNotification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							//sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
							
							$r1[]= Doctrine::getTable('Contacts')->getFriendsContacts($contact_id['contact_id'],$memid);
							
						}
						//echo '<pre>';print_r($r1);exit;
						foreach($r1 as $r1)
						{
							foreach($r1 as $r2)
							{
								if($r2['contact_id']!=" ")
								{
									$is_owner=0; $group_id=0;
									$result= Doctrine::getTable('PhotoRelations')->checkPhotoRelation($photo_id,$r2['contact_id']);
									//echo '<pre>';print_r();exit;
									if($result['numCount']==0)
									{
										$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$r2['contact_id'],$is_owner,$group_id=0,$data['event']);
										//echo $r."   ".$r1;exit;
									}
									//$notifications = Doctrine::getTable('Notifications')->getNotifications($r2['contact_id']);
									//if($notifications['others_shared_triggers'] !="off")
									
									$loginLink = $this->CONFIG->serverAddress.'home';
									$Subject = "Trigger notification from EmotionsMirror";
									$Message = 'Hello "'.$r2['first_name'].'",<br/><br/>you have TriggerNotification  in your emotionmirror  <br/><br/>
										<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
											Emirror team';
									global $serverAddress;
									//sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
									
								}
							}
						}
					}
					$this->_redirect("/album/photos/photos-view/".base64_encode($type1	)."/".base64_encode($timediffId));	
			}
	}
	public function saveAction()
	{	
				//echo '<pre>';print_r($_POST);exit;
				$memid = $_SESSION["sess_userid"];
				$timediffId = $this->_getParam('id');
				$type1 = $this->_getParam('current_page');
				$data=$_POST;
				$data['event']=0;
				//echo '<pre>';print_r(base64_encode($type));exit;
				if($_POST['radio'] == "personal")
				{	
					foreach($data['fileNames'] as $photo)
					{
						//echo $photo;exit;
						$type="P";$album_id=0;
						$result = Doctrine::getTable('Photos')->addPhoto($memid,$type,$album_id,$photo,$timediffId);
				if($result)
				{
				$content_type="photos";
				$share_type=1;
				$activity = Doctrine::getTable('EmActivities')->addActivity($result,$content_type,$memid,$share_type);
				}
						$is_owner=1;$group_id=0;
						$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$result,$memid,$is_owner,$group_id,$data['event'],$timediffId);
						//echo $r."   ".$r1;exit;
					}
					$this->_redirect("/album/photos/photos-view/".base64_encode($type1)."/".base64_encode($timediffId));
				}
				else if($_POST['radio'] == "group")
				{
					//$result="";
					foreach($_POST['group'] as $group_id)
					{
						$result[]= Doctrine::getTable('GroupDetails')->getGroupDetailsByGroupId($group_id);
					}
					//echo '<pre>';print_r($result);exit;
				foreach($data['fileNames'] as $photo)
				{
						$type="P";$album_id=0;
						$photo_id = Doctrine::getTable('Photos')->addPhoto($memid,$type,$album_id,$photo,$timediffId);
				if($photo_id)
				{
				$content_type="photos";
				$share_type=2;
				$activity = Doctrine::getTable('EmActivities')->addActivity($photo_id,$content_type,$memid,$share_type);
				}
						$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$memid,1,0,$data['event'],$timediffId);
						$is_owner=0;
					foreach($result as $r1)
					{
						foreach($r1 as $r2)
						{
							$is_owner=0;$album_id=0;
							$res= Doctrine::getTable('PhotoRelations')->checkPhotoRelation($photo_id,$r2['user_id']);
							//echo '<pre>';print_r($res);exit;
							if($res['numCount']==0)
							{
							$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$r2['user_id'],$is_owner,$r2['group_id'],$data['event'],$timediffId);
							//echo $r."   ".$r1;exit;
							$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "Album notification from EmotionsMirror";
							$Message = 'Hello "'.$r2['user_name'].'",<br/><br/>you have AlbumNotification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							//sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
							}
						}
					}
				}
					$this->_redirect("/album/photos/photos-view/".base64_encode($type1)."/".base64_encode($timediffId));
			}

				else if($_POST['radio']="public")
				{
					foreach($data['fileNames'] as $photo)
					{
						$type="P";$album_id=0;
						$photo_id = Doctrine::getTable('Photos')->addPhoto($memid,$type,$album_id,$photo,$timediffId);
				if($photo_id)
				{
				$content_type="photos";
				$share_type=3;
				$activity = Doctrine::getTable('EmActivities')->addActivity($photo_id,$content_type,$memid,$share_type);
				}
						$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$memid,1,0,$data['event'],$timediffId);
						$is_owner=0;
						$result= Doctrine::getTable('Contacts')->getContacts($memid);
						//echo '<pre>';print_r($result);exit;
						foreach($result as $contact_id)
						{
							$is_owner=0; $group_id=0;
							//$notifications = Doctrine::getTable('Notifications')->getNotifications($contact_id['contact_id']);
							//echo'<pre>';print_r($notifications['mytrigger_comments']);exit;
							$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$contact_id['contact_id'],$is_owner,$group_id,$data['event'],$timediffId);
							//echo $r."   ".$r1;exit;
							//if($notifications['others_shared_triggers'] !="off")
							
							$loginLink = $this->CONFIG->serverAddress.'home';
							$Subject = "Album notification from EmotionsMirror";
							$Message = 'Hello "'.$contact_id['first_name'].'",<br/><br/>you have AlbumNotification  in your emotionmirror  <br/><br/>
								<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
									Emirror team';
							global $serverAddress;
							//sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
							
							$r1[]= Doctrine::getTable('Contacts')->getFriendsContacts($contact_id['contact_id'],$memid);
							
						}
						//echo '<pre>';print_r($r1);exit;
						foreach($r1 as $r1)
						{
							foreach($r1 as $r2)
							{
								if($r2['contact_id']!=" ")
								{
									$is_owner=0; $group_id=0;
									$result= Doctrine::getTable('PhotoRelations')->checkPhotoRelation($photo_id,$r2['contact_id']);
									//echo '<pre>';print_r();exit;
									if($result['numCount']==0)
									{
										$r= Doctrine::getTable('PhotoRelations')->addPhotoRelation($album_id,$photo_id,$r2['contact_id'],$is_owner,$group_id=0,$data['event']);
										//echo $r."   ".$r1;exit;
									}
									//$notifications = Doctrine::getTable('Notifications')->getNotifications($r2['contact_id']);
									//if($notifications['others_shared_triggers'] !="off")
									
									$loginLink = $this->CONFIG->serverAddress.'home';
									$Subject = "Trigger notification from EmotionsMirror";
									$Message = 'Hello "'.$r2['first_name'].'",<br/><br/>you have TriggerNotification  in your emotionmirror  <br/><br/>
										<br/><br/><br/>Please click on the following link to login into emirror<br/><a href="'.$loginLink.'">"'.$loginLink.'"</a><br/><br/><br/>Sincerely,<br/>
											Emirror team';
									global $serverAddress;
									//sendMail($r2['email_id'],$Subject,$Message,$_SESSION["sess_email"]);
									
								}
							}
						}
					}
					$this->_redirect("/album/photos/photos-view/".base64_encode($type1)."/".base64_encode($timediffId));	
			}
	}
	public function paginationphotosAction()
	{
		$photo_diffid = base64_decode($_GET['photo_diffid']);
		$type=$this->_getParam('id');
		//$photo_diffid=$this->_getParam('photo_diffid');
		$memid=$_SESSION["sess_userid"];
		//$this->view->params ="";
		//$photo_diffid='1338445902';
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');

		if($type == 2)
		{
		$ideaDetails = Doctrine::getTable('Photos')->getPersonalPhotosByPhotoId($memid,$photo_diffid,$currentPage,$resultsPerPage);
		}
		if($type == 4)
		{
		$ideaDetails = Doctrine::getTable('Photos')->geSharetoMePhotosByPhotoId($memid,$photo_diffid,$currentPage,$resultsPerPage);
		}
		$this->view->photoview = $ideaDetails;
		$this->view->imgpath= '/'.$this->CONFIG->path->albumPhotosBig;
		$this->view->photo_diffid =$photo_diffid;
		$this->view->photo_id=$ideaDetails['result'][0]['photo_id'];
		$this->view->type1=0;
		$this->view->type=$type;
		$jsonData['output'] = $this->view->render('photos/photo-view-list.html');
		$jsonData['pages'] = $ideaDetails['pages'];
	    $jsonData['pager'] = $ideaDetails['pager'];
	    $jsonData['currentPage'] = $ideaDetails['currentPage'];
		$jsonData['resultsCount'] = $ideaDetails['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		//echo '<pre>';print_r($ideaDetails);exit;
		
		
		
	}
	
	/// FUNCTIONALITY FOR PHOTO VIEW LARGEEE#########################################################
	
	public function photoViewLargeAction()
	{
			$type=base64_decode($this->_getParam('id'));
			$photo_diffid=base64_decode($this->_getParam('current_page'));
			
			if($this->_getParam('current_page') !='') {
				$current_photo = $this->_getParam('results_per_page');
			} else {
				$current_photo = 1;
			}
			
			
			//echo $trigger_id;exit;
			$memid = $_SESSION["sess_userid"];
			$read_status= Doctrine::getTable('PhotoRelations')->updatePhotoReadStatus($memid,$photo_diffid,1);
			$this->view->params ="";
			if($type == 2)
			{
				$photo_result= Doctrine::getTable('Photos')->getPersonalPhotosByPhotoDiffid($memid,$photo_diffid);
			} else if($type == 4)
			{
				$photo_result= Doctrine::getTable('Photos')->geSharetoMePhotosByPhotoDiffid($memid,$photo_diffid);
				
			}
			//echo '<pre>';print_r($photo_result);exit;
			$this->view->photoview=$photo_result;
			$photo_id=$photo_result[0]['photo_id'];
			$addedby=$photo_result[0]['addedby'];
			$photo_description=$photo_result[0]['description'];
			$photo=$photo_result[0]['photo'];
			$is_owner=0;
			$this->view->comments_allow=$photo_result[0]['comments_allow'];
			$this->view->photo_description=$photo_description;
			$this->view->photo=$photo;
			$this->view->addedby=$addedby;
			$this->view->is_owner=$is_owner;
			$this->view->photo_diffid=$photo_result[0]['photo_diffid'];
			$this->view->added_date=$photo_result[0]['added_date'];
			$this->view->photo_id=$photo_id;
			$comments= Doctrine::getTable('EmComments')->getComments($photo_id,1,4);
			$this->view->getComments=$comments;
			//echo '<pre>';print_r($comments);exit;
			$this->view->type1=0;
			$this->view->type=$type;
			$this->view->current_photo=$current_photo;	
			
			
	}
	public function submitCommentAction()
	{	
		//echo '<pre>';print_r($_POST);exit;
		$memid = $_SESSION["sess_userid"];
		$_POST['album_id']=0;
			$result= Doctrine::getTable('EmComments')->addComments($memid,$_POST);
			//echo '<pre>';print_r($result);exit;
			$comments= Doctrine::getTable('EmComments')->getComments($_POST['photo_id'],1,4);
			$result= Doctrine::getTable('Photos')->getAlbumPhotosByPhotoId($_POST['photo_id']);
			$this->view->getComments=$comments;
			$this->view->photo_id=$_POST['photo_id'];
			$this->view->is_owner=$_POST['is_owner'];
			$this->view->photo_description=$result['description'];
			$jsonData['output'] = $this->view->render('index/photo-comments.html');
			//echo '<pre>';print_r($comments);exit;
			$this->_helper->json->sendJson($jsonData);
	}
		public function deleteCommentAction()
	{
		//echo '<pre>';print_r($_POST);exit;
		$memid=$_SESSION["sess_userid"];
	  	$comment_id = $_POST['comment_id'];
		$album_id = 0;
		$type = $_POST['type'];
		$photo_id=$_POST['photo_id'];
		$currentPage=$_POST['current_page'];
		$result = Doctrine::getTable('EmComments')->deleteComment($comment_id);
			$comments= Doctrine::getTable('EmComments')->getComments($photo_id,$currentPage,4);
			//echo '<pre>';print_r($comments);exit;
			$this->view->getComments=$comments;
			$this->view->photo_id=$photo_id;
			$this->view->is_owner=$_POST['is_owner'];
			$result= Doctrine::getTable('Photos')->getAlbumPhotosByPhotoId($photo_id);
			$this->view->photo_description=$result['description'];
			$jsonData['output'] = $this->view->render('index/photo-comments.html');
		$this->_helper->json->sendJson($jsonData);	
	}

}	
